﻿using System;
using System.Collections.Generic;
using System.Data;
using WebProject.Core.Models;
using WebProject.Core.Repositories;

namespace WebProject.Core.Service
{
    public class NewsService : INewsService
    {
        #region[Private Attribute]

        private readonly INewsRepository m_NewsRepository;

        #endregion

        #region[Constructor]

        public NewsService(INewsRepository newsRepository)
        {
            if (newsRepository == null)
            {
                throw new ArgumentNullException("newsRepository");
            }
            m_NewsRepository = newsRepository;
        }

        #endregion

        #region[Implement Methods]
        public int Add(News news)
        {
            if (news == null)
            {
                throw new ArgumentNullException("news");
            }
            return m_NewsRepository.Add(news);
        }

        public bool Remove(News news)
        {
            if (news == null)
            {
                throw new ArgumentNullException("news");
            }
            return m_NewsRepository.Remove(news) != 0;
        }

        public bool Remove(int id)
        {
            var target = m_NewsRepository.GetById(id);
            if (target == null)
            {
                throw new ObjectNotFoundException(string.Format("Target News with Id={0} not found", id));
            }
            return m_NewsRepository.Remove(target) != 0;
        }

        public bool Update(News news)
        {
            if (news == null)
            {
                throw new ArgumentNullException("news");
            }
            return m_NewsRepository.Update(news) != 0;
        }

        public IList<News> GetAllNews()
        {
            return m_NewsRepository.GetAll();
        }

        public News GetNewsById(int id)
        {
            return m_NewsRepository.GetById(id);
        }
        #endregion
    }
}